package controller

import (
	"hft/Database/admin"
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Audit struct{}

func (*Audit) GetAccountAuditList(c *gin.Context) {

	var paginate request.AccountAuditPage

	c.ShouldBind(&paginate)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.AuditService.GetAccountAuditList(&paginate, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

func (*Audit) OperaAccountAudit(c *gin.Context) {
	var data *admin.AccountAudit

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	if data.Result != 2 && data.Result != 3 {
		utils.FailWithMessage("无效的请求", c)
		return
	}

	err = services.AuditService.OperaAccountAudit(int(data.ID), data.Result, data.Advice, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Audit) GetSingleAccountAuditInfo(c *gin.Context) {

	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))

	data, err := services.AuditService.GetSingleAccountAuditInfo(id)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)

}

var AuditController = new(Audit)
